package com.heima.wemedia.runner;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class WmNewsApplicationRunner implements ApplicationRunner {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //2. 初始化DFA数据结构
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(Wrappers.lambdaQuery(WmSensitive.class).select(WmSensitive::getSensitives));
        List<String> stringList = wmSensitives.stream().map(wmSensitive -> wmSensitive.getSensitives()).collect(Collectors.toList());

        SensitiveWordUtil.initMap(stringList);
    }
}